package com.pirestupppai.widgets.dialog;

import android.app.Dialog;
import android.content.Context;
import android.media.MediaPlayer;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.RelativeLayout;
import android.widget.VideoView;

import com.pirestupppai.R;
import com.pirestupppai.common.Constant;
import com.pirestupppai.utils.Utils;

/**
 * 视频播放
 *
 * author: xiaoluo
 * date: 2017/5/18 14:50
 */
public class VideoDialog extends Dialog{
    private Context mContext;
    private String mVideoPath;

    private VideoView mVideoView;
    private RelativeLayout mParentLayout;

    public VideoDialog(Context context, String url) {
        super(context, R.style.VideoDialog);
        mContext = context;
        mVideoPath = url;
        setContentView(R.layout.dialog_video);

        Window menuWindow = getWindow();
        menuWindow.getAttributes().width = mContext.getResources().getDisplayMetrics().widthPixels;
        menuWindow.getAttributes().height = Constant.HEIGHT_OF_SCREEN;
        menuWindow.setGravity(Gravity.CENTER_VERTICAL);
        setCanceledOnTouchOutside(true);

        mVideoView = (VideoView) findViewById(R.id.origin_video);
        mParentLayout = (RelativeLayout) findViewById(R.id.parent_layout);

        RelativeLayout.LayoutParams videoParams = (RelativeLayout.LayoutParams) mVideoView.getLayoutParams();
        videoParams.width = Constant.WIDTH_OF_SCREEN;
        videoParams.height = Constant.WIDTH_OF_SCREEN;
        mVideoView.setLayoutParams(videoParams);

        if (!TextUtils.isEmpty(mVideoPath)) {
            mVideoView.setVideoPath(mVideoPath);
        }

        mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                Utils.showToast("Error:" + what);
                return false;
            }
        });

        mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mVideoView.start();
            }
        });

        mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mVideoView.start();
            }
        });

        mParentLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();

            }
        });
    }

}
